﻿using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using E4RPDevExpress10;
using StationCommon;

namespace Station.Controls
{
    public partial class WorkOrderItemList : UserControl, IGridView
    {
        private Controller _controller;

        #region Properties

        public Controller Controller
        {
            set
            {
                _controller = value;
                _controller.WorkSheetSaved += delegate { SetGridDataSource(); };
                _controller.MechanicSaved += delegate { SetGridDataSource(); };
                SetGridDataSource();
            }
        }

        #endregion

        public WorkOrderItemList()
        {
            InitializeComponent();
            FormatGridColumns();
        }

        #region Private Methods

        private void FormatGridColumns()
        {
            DevExpressHelper.SetColumnAsCurrency(colValorUnitario);
            DevExpressHelper.SetColumnAsCurrency(colValorBase);
            DevExpressHelper.SetColumnAsNumeric(colPercentagemDesconto);
            DevExpressHelper.SetColumnAsCurrency(colValorDesconto);
            DevExpressHelper.SetColumnAsCurrency(colValorSemIva);
            DevExpressHelper.SetColumnAsNumeric(colPercentagemIva);
            DevExpressHelper.SetColumnAsCurrency(colValorIva);
            DevExpressHelper.SetColumnAsCurrency(colValorComIva);
            var gridColumns = new List<GridColumn>
                                  {
                                      colValorUnitario,
                                      colValorBase,
                                      colValorDesconto,
                                      colValorSemIva,
                                      colValorIva,
                                      colValorComIva
                                  };
            DevExpressHelper.ShowGroupSummaryAsCurrency(gridView, gridColumns);
        }

        private void SetGridDataSource()
        {
            var profile = DevExpressHelper.SaveLayoutToStream(gridView);
            ListaItemFolhaObraDataSet dataSet = _controller.GetWorkOrderItemList();
            gridControl.DataSource = null;
            gridControl.DataSource = dataSet;
            gridControl.DataMember = dataSet.ItemFolhaObra.TableName;
            DevExpressHelper.RestoreLayoutFromStream(profile, gridView);
            FormatGridColumns();
        }

        #endregion

        #region IGridView

        public GridView GridView()
        {
            return gridView;
        }

        public string ListName
        {
            get { return "Station:WorkOrderItemList"; }
        }

        #endregion
    }
}
